package com.mtao.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mtao.dto.BannerDto;
import com.mtao.BannerService;
import com.mtao.mapper.BannerMapper;
import com.mtao.entity.Banner;
import com.mtao.PageBean;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BannerServiceImpl implements BannerService {

    @Resource
    private BannerMapper bannerMapper;


    /**
     * 添加banner
     * @param banner
     */
    @Override
    public void addBanner(Banner banner){
        bannerMapper.addBanner(banner);
    }

    /**
     * 分页查询
     * @param bannerDto
     * @return
     */
    @Override
    public PageBean<Banner> bannerList(BannerDto bannerDto) {
        //1.创建PageBean
        PageBean<Banner> pageBean = new PageBean<>();
        //2.开启分页查询 PageHelper
        PageHelper.startPage(bannerDto.getPage(), bannerDto.getSize());
        List<Banner> list=null;
        //3.调用mapper
        if(bannerDto.getType().isEmpty()){
             list =bannerMapper.list(null);
        }else{
            list =bannerMapper.list(bannerDto.getType());
        }
        System.out.println(list);
        //Page 中提供了方法,可以获取PageHelper分页查询后 得到的总记录条数和当前页数据
        Page<Banner> p =(Page<Banner>) list;
        //把数据填充到pageBeanBanner对象里面
        pageBean.setTotal(p.getTotal());
        pageBean.setList(p.getResult());
        return pageBean;
    }

    @Override
    public void deleteBanner(int anInt) {
        bannerMapper.delete(anInt);
    }

    /**
     * 查询type所有
     * @param type
     * @return
     */
    @Override
    public List<Banner> bannerAllList(String type) {
        return bannerMapper.bannerAllList(type);
    }
}
